<?php
namespace Sdk\GoodCang;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;


class callGCApiUtil
{
    private const URL = "https://oms.goodcang.net";

    private const APP_KEY = "09a41800b013e4b348805328a38fb2d1";
    private const APP_TOKEN = "df345f56a6200adcb959ac857884663e";

    public static function callGCApi($action, $method, $data)
    {
        try {
            $client = new Client();
            $url = self::URL . $action;
            $headers = [
                'app-key' => self::APP_KEY,
                'app-token' => self::APP_TOKEN,
                'Content-Type' => 'application/json',
                'chraset' => 'UTF-8',
                'Accept' => 'application/json'
            ];
            if ('post' == strtolower($method)) {
                $response = $client->request($method, $url, ['headers' => $headers, 'json' => $data]);
            } else {
                $arr = [];
                foreach ($data as $k => $v) {
                    $arr[] = "$k=$v";
                }
                $url .= "?".implode("&", $arr);
                $response = $client->request($method, $url, ['headers' => $headers]);
            }
            $status = $response->getStatusCode();
            if (200 == $status) {
                $result = $response->getBody()->getContents();
                return json_decode($result, true);
            } else {
                return [];
            }
        } catch (ClientException $e) {
            return [];
        }
    }

}